package com.piesat.quake.business.device.mqtt.reader;

import com.piesat.quake.business.device.mqtt.NumberType;
import com.piesat.quake.business.device.mqtt.param.NumberParseParam;
import com.piesat.quake.business.device.mqtt.param.ParseParam;

import io.netty.buffer.ByteBuf;

/**
 * 基本数据读取器
 *
 * @author Gersy
 * @date 2023/2/6
 * @since 1.0
 */
public class NumberReader implements Reader {
    /**
     * 硬件商的定义
     */
    public static final int LITTLE_ENDIAN = 1;

    @Override
    public Object read(ByteBuf byteBuf, ParseParam numberParam) {
        NumberParseParam param = (NumberParseParam) numberParam;
        int type = param.numberType;
        if (type == NumberType.BYTE) {//byte
            return byteBuf.readByte();
        } else if (type == NumberType.INT) {//int
            return param.endian == LITTLE_ENDIAN ? byteBuf.readIntLE() : byteBuf.readInt();
        } else if (type == NumberType.FLOAT) {//float
            return param.endian == LITTLE_ENDIAN ? byteBuf.readFloatLE() : byteBuf.readFloat();
        } else if (type == NumberType.SHORT) {//short
            return param.endian == LITTLE_ENDIAN ? byteBuf.readShortLE() : byteBuf.readShort();
        } else if (type == NumberType.LONG) {//long
            return param.endian == LITTLE_ENDIAN ? byteBuf.readLongLE() : byteBuf.readLong();
        } else if (type == NumberType.DOUBLE) {//double
            return param.endian == LITTLE_ENDIAN ? byteBuf.readDoubleLE() : byteBuf.readDouble();
        }
        return null;
    }
}
